package com.zhb.services;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhb.entity.UserInfo;
import com.zhb.util.HtmlUtils;
import com.zhb.util.HttpUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.HttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by jarvan4dev on 16/5/18.
 */
public class UserService {

    private final HttpClient httpClient;

    private static final String loginLibUri = "http://27.191.198.3:7888/action/web/login.do";
    private static final String loginOpacUri = "http://27.191.198.3:7088/opac/reader/doLogin";
    private static final String profileUri = "http://27.191.198.3:7888/action/web/integral.do?actionCmd=editActionReader";
    private static final String indexUri = "http://27.191.198.3:7088/opac/reader/space";
    // 必须调用此函数用于初始化httpClient, 且httpClient是全局唯一的
    public UserService(HttpClient httpClient){
        this.httpClient = httpClient;
    }

    // 通过用户名密码进行登录, 图书馆系统
    public void doLoginLib(String username, String password) throws IOException {
        Map<String, Object> params = Maps.newHashMap();
        params.put("cmdACT", "validateReader");
        params.put("password", password);
        params.put("rdpasswd", DigestUtils.md5Hex(password));
        params.put("rdid", username);
        HttpUtils.doPost(httpClient, loginLibUri, params);

    }

    // 登录opac系统
    public void doLoginOpac(String username, String password) throws IOException {
        Map<String, Object> params = Maps.newHashMap();
        params.put("password", "");
        params.put("rdPasswd", DigestUtils.md5Hex(password));
        params.put("rdid", username);
        params.put("returnUrl", "");
        HttpUtils.doPost(httpClient, loginOpacUri, params);
        // 刷新页面
        HttpUtils.doGet(httpClient, indexUri);
    }

    // 访问个人信息页面
    public String visitProfilePage() throws IOException {
        return HttpUtils.doGet(httpClient, profileUri);
    }

    // 解析HTML获取用户信息
    public UserInfo getUserInfo(String htmlSource){
        List<String> infoList = Lists.newArrayList();
        String formatHtml = HtmlUtils.formatHtml(htmlSource);
        Document document = Jsoup.parse(formatHtml);
        // 获取最后一个table, 即为包裹个人信息的table
        Element tborder = document.getElementsByTag("table").last();
        Elements infoTds = tborder.getElementsByClass("td2");
        // 遍历每一行table
        for (Element infoTd : infoTds){
            //形如: <td class="td2">&nbsp;<b>0004989</b></td>
            Element tmp = infoTd.children().first();
            if (tmp == null){
                infoList.add(infoTd.text().trim());
            } else {
                Elements inputs = infoTd.select("input[type=text]");
                for (Element input : inputs) {
                    infoList.add(input.val());
                }
            }
        }
        return assembleUserInfo(infoList);
    }

    private UserInfo assembleUserInfo(List<String> infoList){
        UserInfo userInfo = new UserInfo();
        userInfo.setAccount(infoList.get(0));
        userInfo.setName(infoList.get(1));
        userInfo.setGender(infoList.get(2));
        userInfo.setID(infoList.get(3));
        userInfo.setAccountLib(infoList.get(4));
        userInfo.setTel(infoList.get(5));
        userInfo.setMobile(infoList.get(6));
        userInfo.setEmail(infoList.get(7));
        userInfo.setAddress(infoList.get(8));
        userInfo.setComSchool(infoList.get(9));
        return userInfo;
    }
}
